package com.ruoyi.order.service.impl;

import java.util.HashMap;
import java.util.List;

import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.system.api.RemoteProductService;
import com.ruoyi.system.api.RemoteProductsService;
import com.ruoyi.system.api.domain.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Map;

import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.order.domain.Details;
import com.ruoyi.order.mapper.OrderByMapper;
import com.ruoyi.order.domain.OrderBy;
import com.ruoyi.order.service.IOrderByService;

/**
 * 订单Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-10-12
 */
@Service
public class OrderByServiceImpl implements IOrderByService 
{
    @Autowired
    private OrderByMapper orderByMapper;

    @Autowired
    private RemoteProductsService productsService;
    /**
     * 查询订单
     * 
     * @param orderId 订单主键
     * @return 订单
     */
    @Override
    public OrderBy selectOrderByByOrderId(Long orderId)
    {
        return orderByMapper.selectOrderByByOrderId(orderId);
    }

    /**
     * 查询订单列表
     * 
     * @param orderBy 订单
     * @return 订单
     */
    @Override
    public List<OrderBy> selectOrderByList(OrderBy orderBy)
    {
        return orderByMapper.selectOrderByList(orderBy);
    }

    /**
     * 新增订单
     * 
     * @param orderBy 订单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertOrderBy(OrderBy orderBy)
    {
        int rows = orderByMapper.insertOrderBy(orderBy);
        insertDetails(orderBy);
        return rows;
    }

    /**
     * 修改订单
     * 
     * @param orderBy 订单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateOrderBy(OrderBy orderBy)
    {
        orderByMapper.deleteDetailsByOrderDetailsId(orderBy.getOrderId());
        insertDetails(orderBy);
        return orderByMapper.updateOrderBy(orderBy);
    }

    /**
     * 批量删除订单
     * 
     * @param orderIds 需要删除的订单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOrderByByOrderIds(Long[] orderIds)
    {
        orderByMapper.deleteDetailsByOrderDetailsIds(orderIds);
        return orderByMapper.deleteOrderByByOrderIds(orderIds);
    }

    @Override
    public List<OrderBy> selectOrderByUserId(Long userId) {
        return orderByMapper.selectOrderByUserId(userId);
    }

    //店铺结算获得订单详情
    @Override
    public List<OrderBy> getOrderDetail(Long shopsId) {
        List<Product> productByShopsId = productsService.getProductByShopsId(shopsId, SecurityConstants.INNER);
        if (!productByShopsId.isEmpty()){
            List<OrderBy> order = orderByMapper.getOrderDetail(productByShopsId);
            return order;
        }else {
            return null;
        }

    }

    @Override
    public List<OrderBy> selectOrderByByStatus(Long status) {

        return orderByMapper.selectOrderByByStatus(status);
    }

    /**
     * 删除订单信息
     * 
     * @param orderId 订单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOrderByByOrderId(Long orderId)
    {
        orderByMapper.deleteDetailsByOrderDetailsId(orderId);
        return orderByMapper.deleteOrderByByOrderId(orderId);
    }

    /**
     * 新增订单详情信息
     * 
     * @param orderBy 订单对象
     */
    public void insertDetails(OrderBy orderBy)
    {
        List<Details> detailsList = orderBy.getDetailsList();
        Long orderId = orderBy.getOrderId();
        if (StringUtils.isNotNull(detailsList))
        {
            List<Details> list = new ArrayList<Details>();
            for (Details details : detailsList)
            {
                details.setOrderDetailsId(orderId);
                list.add(details);
            }
            if (list.size() > 0)
            {
                orderByMapper.batchDetails(list);
            }
        }
    }
}
